<html>
<head>
<script type="text/javascript">
	var aJaxURL			= "server-side/operations/producing.action.php";					//server side folder url
	var list_aJaxURL	= "server-side/operations/producing/list.action.php";		//server side folder url
	var tName			= "example";														//table name
	var fName			= "add-edit-form";													//form name
	var list_fName		= "add-edit–cartridge-form";										//form name
	var localID			= "";
	status          = "1" ;	
			
	$(document).ready(function () {

	    $( "#radio" ).buttonset()
	    $("input[name='radio']").on("change", function () {
	       status=this.value;
	       LoadTable();
	    });

		
		LoadTable();						
		/* Add Button ID, Delete Button ID */
		SetEvents("", "", "", tName, fName , aJaxURL);
		$(element).focus(function() {
    		newWin.close();
    		});
	});
	
	function LoadTable(table){
		switch(table){
			case 'list' :
				var id = $("#producId").val();
				var menuLength	= [[10], [10]];
				GetButtons("barcode_input1", "");				
				/* Table ID, aJaxURL, Action, Colum Number, Custom Request, Hidden Colum, Menu Array */
				GetDataTable("prod_list", list_aJaxURL, "get_list", 4, "id="+id, 0, menuLength, 1, "asc");
				SetEvents("", "", "", "prod_list" , "add-edit–cartridge-form", list_aJaxURL);
				$("#barcode_input1").focus();
				break;
			default		:
				GetButtons("barcode_input", "");
			GetDataTable_not_sort(tName, aJaxURL, "get_list&status="+status, 6, "", 0,  "", 2, "asc");
				$("#barcode_input").focus();	
				break;	
		}	
				
	}

	function LoadDialog(form){

		if(this.parent.status=="1"){
			text="გასარკვევში გადატანა ";
			st=2;		
			  }else{
			text="გასარკვევიდან გადმოტანა";
			st=1;
				  }

		
		switch (form) {
			case "add-edit–cartridge-form" :
				var buttons = {
			        "test1": {
		                text: "დატესტვა(სრული)",
		                id: "test-dialog1",
		                click: function () {
							var detid = $("#warehouseDetid").val();
							
		        	    	var link = GetRootDIR();
		        	    	
		        	    	link = link + "server-side/print/testpage.php?id=" + detid;
		        	    	
		        	    	var newWin = window.open(link, "JSSite", "resizable=yes,scrollbars=yes,status=yes");
		        	    	newWin.focus();
		        	    	newWin.onload = function() {
		        	    		newWin.print();
		        	    	}
		        	    	
		        	    	setInterval(function() {
		        	    		newWin.close();
		        	    	}, 500);
		        	    	
		                }
			        },
			        "test": {
		                text: "დატესტვა",
		                id: "test-dialog",
		                click: function () {
							var detid = $("#warehouseDetid").val();
							
		        	    	var link = GetRootDIR();
		        	    	
		        	    	link = link + "server-side/print/test_page.action.php?id=" + detid;
		        	    	
		        	    	var newWin = window.open(link, "JSSite", "resizable=yes,scrollbars=yes,status=yes");
		        	    	newWin.focus();
		        	    	newWin.onload = function() {
		        	    		newWin.print();
		        	    	}
		        	    	
		        	    	setInterval(function() {
		        	    		newWin.close();
		        	    	}, 500);
		        	    	
		                }
			        },
			        "save": {
		                text: "დასრულება",
		                id: "save-dialog",
		                click: function () {
			            	param 		= new Object();
			            	param.act	= 'end_cartridge_producing';			            	
			            	param.id 	= $("#warehouseDetid").val();
						    $.ajax({
						        url: list_aJaxURL,
							    data: param,
						        success: function(data) {
									if(typeof(data.error) != "undefined"){
										if(data.error != ""){
											alert(data.error);
										}else{												
											LoadTable("list");
											$("#"+list_fName).dialog("close");																							
										}
									}
							    }
						    });							    
		                }
	                },				        
			        "cancel": {
		                text: "დახურვა",
		                id: "cancel-dialog",
		                click: function () {
		                	$(this).dialog("close");
		                	$("#barcode_input1").focus();
		                }				        
		            }
				};
				GetDialog(list_fName, 550, "auto",buttons);
				break;
			default :
				var buttons = {
			        "print_blank": {
		                text: "ბლანკის ბეჭდვა",
		                id: "print_blank",
		                click: function () {
		                	var id = $("#producId").val();
		                	
		        	    	var link = GetRootDIR();
							
		        	    	if($("#letterStatus").val() == 2 ){
			        	    	link = link + "server-side/print/pwork_blank.php?id="+id;
		        	    	}else if($("#letterStatus").val() == 1 ) {
			        	    	link = link + "server-side/print/cwork_blank.php?id="+id;			        	    	
		        	    	}else{
			        	    	link = link + "server-side/print/rwork_blank.php?id="+id;		        	    	
			        	    	
		        	    	}		        	    	
		        	    	
		        	    	var newWin = window.open(link, "JSSite", "width=800,height=800,resizable=yes,scrollbars=yes,status=yes");
		        	    	//newWin.focus();
		        	    	newWin.onload = function() {
		        	    		newWin.print();
		        	    	}
		        	    	newWin.focus();
		        	    	setInterval(function() {
		        	    		newWin.close();
		        	    	}, 500);
		                }
		            },
		            
			        "move_unk": {				        
		                text: text,
		                id: "move_unk",
		                click: function () {
		                    data= { act :"unknown_move",
			                		id  :$("#producId").val(),
			                		status:st   
	    			                }				    	
		                	$.getJSON(aJaxURL, data, function( data ) {
			                	 LoadTable();
			                	
			                	});	
			                
		                	$(this).dialog("close");		                	
		                }
		            },
			        "end": {
		                text: "წარმოების დასრულება",
		                id: "end_producing",
		                click: function () {
			            	param 		= new Object();
			            	param.act	= 'end_producing';			            	
			            	param.id 	= $("#producId").val();
			    	    	param.type  = $("#letterStatus").val();			    
						    $.ajax({
						        url: aJaxURL,
							    data: param,
						        success: function(data) {
									if(typeof(data.error) != "undefined")
									{
										if(data.error != "")
										{
											alert(data.error);
										}else
										{												
											LoadTable();
											$("#"+fName).dialog("close");												
										}
									}
							    }
						    });				                
		                	
		                }
		            },					
			        "recive": {
		                text: "მიღება",
		                id: "recive",
		                click: function () {
		            		var id = $("#producId").val();
		            		var type = $("#letterStatus").val();
		            		
		                    $.ajax({
		                        url: aJaxURL,
		                        data: "act=recive&id="+id+"&type="+type,
		                        success: function(data) {
		                            if (typeof(data.error) != "undefined") {
		                                if (data.error != "") {
		                                    alert(data.error);
		                                }else{
		                                	$("#recive").css("display", "none");
		            						$("#print_blank").css('display','inline-block');					
		            						$("#end_producing").css('display','inline-block');
		                                }
		                            }
		                        }
		                    });
		                	LoadTable();
		                }
		            },			        
			        "cancel": {
		                text: "დახურვა",
		                id: "cancel-dialog",
		                click: function () {
		                	$(this).dialog("close");
		                	$("#barcode_input").focus();
		                }
		            }
				};
				GetDialog(fName, 700, "auto",buttons);
				var status = $("#produsingStatus").val();
				switch(status){
					case "0" :
						$("#print_blank").css("display", "none");						
						$("#end_producing").css("display", "none");
						$("#recive").css('display','inline-block');
						
					  break;		  			  			  
					default:
						$("#print_blank").css('display','inline-block');					
						$("#end_producing").css('display','inline-block');
						$("#recive").css("display", "none");
					  break;
				}
				LoadTable('list');
				break;
		}

	}
	
	$(document).on("keydown", "#barcode_input", function (event) {
		if (event.keyCode == $.ui.keyCode.ENTER){
			param 			= new Object();
        	param.act		= 'get_warehouse_id';
        	param.barcode	= $("#barcode_input").val();
        	
			if(param.barcode.length == 15){
			    $.ajax({
			        url: aJaxURL,
				    data: param,
			        success: function(data) {
						if(typeof(data.error) != "undefined"){
							if(data.error != ""){
								alert(data.error);
							}else{
	
								param1 			= new Object();
					        	param1.act		= 'get_edit_page';
					        	param1.id	= data.warehouseID;
							    $.ajax({
							        url: aJaxURL,
								    data: param1,
							        success: function(data) {
										if(typeof(data.error) != "undefined"){
											if(data.error != ""){
												alert(data.error);
											}else{
												$("#"+fName).html(data.page);
												LoadDialog(fName);
												
												GetCartidgeDialog(param.barcode)																		
											}
										}
								    }
							    });						    
															
							}
						}
				    }
			    });
			}
		    $("#barcode_input").val("");
			event.preventDefault();					//Clear Dialog Default <b>keypress<b> Event
        }
	});

	$(document).on("keydown", "#barcode_input1", function (event) {
		if (event.keyCode == $.ui.keyCode.ENTER){
			
			GetCartidgeDialog( $("#barcode_input1").val() );
			$("#barcode_input1").val("");		
			event.preventDefault();					//Clear Dialog Default <b>keypress<b> Event
        }
	});
		
	function GetCartidgeDialog(barcode){
    	param 			= new Object();
    	param.act		= 'get_edit_page';
    	param.barcode	= barcode;
    	param.id	= $("#producId").val();
    	
    	if(param.barcode.length == 15){
		    $.ajax({
		        url: list_aJaxURL,
			    data: param,
		        success: function(data) {
					if(typeof(data.error) != "undefined"){
						if(data.error != ""){
							alert(data.error);
						}else{			
								$("#add-edit–cartridge-form").html(data.page);
								LoadDialog("add-edit–cartridge-form");																						
						}
					}
			    }
		    });
    	}else{
    		alert("შტრიხკოდი არასწორია");
    	} 		
	}
</script>

<style type="text/css">
    
 .radio {
  	
  background:#C9C9C9 !important;
  border:none !important;	
  color: #fff !important;  	
}

.ui-button.ui-state-active .ui-button-text {
  background-image: linear-gradient(to bottom, #8aaaff, #006db0) !important;
  color: #fff; 
  border-radius:5px; 	
}    
    
</style>

</head>

<body>
    <div id="dt_example" class="ex_highlight_row">
        <div id="container">
            <div id="dynamic">
                <h2 align="center">ტესტერის გვერდი</h2>   
	        	<div id="button_area">
	        	 <div id="radio" style="  display: inline-block;">
                    <input type="radio" id="radio2" value="1" name="radio" checked><label for="radio2">ყველა </label>   
                    <input type="radio" id="radio1" value="2" name="radio"><label for="radio1">გასარკვევები</label> 
                  </div>	        		<input style="display:inline; " id="barcode_input" type="text" maxlength="15"/>
	        	</div>
                <table class="display" id="example">
                    <thead>
                        <tr id="datatable_header">
                            <th>ID</th>
                            <th class="min" title="შექმნის დრო">დაწყების დრო</th>                           
                            <th style="width: 170px" title="დასრულების გეგმიური დრო">დას. გეგმიური დრო</th>
                            <th class="min">ბარათის ნომერი</th>
                            <th style="width: 100%">ორგანიზაცია</th>
                            <th style="width: 190px">სტატუსი</th>
                        </tr>
                    </thead>
                    <thead>
                        <tr class="search_header">
                            <th class="colum_hidden">
                            	<input type="text" name="search_id" value="ფილტრი" class="search_init" />
                            </th>
                      		<th>
                            	<input type="text" name="search_startdate" value="ფილტრი" class="search_init" />
                            </th>                            
                            <th>
                                <input type="text" name="search_enddate" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_aleter" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_client" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_client" value="ფილტრი" class="search_init" />
                            </th>
                        </tr>
                    </thead>
                </table>
            </div>
            <div class="spacer">
            </div>
        </div>
    </div>
     <div id="add-edit-form" class="form-dialog" title="ბლანკი">
    	<!-- aJax -->
	</div>    
</body>
</html>